﻿@{
    Layout = "~/Views/Shared/_LayoutBase.cshtml";
}
@section Script{
<script type="text/javascript">
    jQuery(function () {
        var op = $("#OldPassword"), np = $("#NewPassword"), cp = $("#ConfirmPassword"), allFields = $([]).add(op).add(np).add(cp), tips = $(".validateTips");
        function checkLength(o, n, min, max) {
            if (o.val().length > max || o.val().length < min) {
                o.addClass("ui-state-error");
                updateTips("长度 " + n + " 必须在 " + min + " 和 " + max + ".");
                return false;
            }
            else {
                return true;
            }
        }
        function updateTips(t) {
            tips.text(t).addClass("ui-state-highlight");
            setTimeout(function () {
                tips.removeClass("ui-state-highlight", 1500);
            }, 500);
        }
        function checkPassword(n, c) {
            if (n.val() == c.val()) {
                return true;
            }
            else {
                c.addClass("ui-state-error");
                updateTips("密码不一致");
            }
        }
        $("#change-password").dialog({
            autoOpen: false,
            height: 380,
            width: 350,
            modal: true,
            buttons:
        {
            "确定": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(op, "当前密码", 6, 16);
                bValid = bValid && checkLength(np, "新密码", 6, 16);
                bValid = bValid && checkLength(cp, "确认密码", 6, 16);

                bValid = bValid && checkPassword(np, cp);

                if (bValid) {
                    $.post('/account/changepassword',
                    {
                        OldPassword: op.val(),
                        NewPassword: np.val(),
                        ConfirmPassword: cp.val()
                    }, function (data) {
                        if (data.success) {
                            Alert("恭喜", "修改密码成功！");
                            $("#change-password").dialog("close");
                        }
                        else {
                            updateTips(data.message);
                        }
                    });
                }
            },
            "取消": function () {
                $(this).dialog("close");
            }
        },
            close: function () {
                allFields.val("").removeClass("ui-state-error");
            }
        });
    });
</script>
}
@section Content{
<div id="change-password" title="修改密码" style="display: none;">
    <p class="validateTips">所有元素都需要填写</p>
    <fieldset>
        <label for="OldPassword">当前密码</label>
        <input type="password" name="OldPassword" id="OldPassword" class="text ui-widget-content ui-corner-all" />
        <label for="NewPassword">新密码</label>
        <input type="password" name="NewPassword" id="NewPassword" value="" class="text ui-widget-content ui-corner-all" />
        <label for="ConfirmPassword">确认密码</label>
        <input type="password" name="ConfirmPassword" id="ConfirmPassword" value="" class="text ui-widget-content ui-corner-all" /> </fieldset>
</div>
}